package com.hetwen.remoteboot.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

import com.hetwen.remoteboot.db.DBOpenHelper;

public class MacProvider extends ContentProvider {

	private static final String AUTHORITY = "com.hetwen.remoteboot";
	private static final String TBNAME = "mac_addresses";
	private static final int    MACADDR = 0;
	private static final int    MACADDR_ID  = 1;
	private static final int    MACADDR_NAME  = 2;
	private static final int    MACADDR_MAC  = 3;
	
	private SQLiteOpenHelper helper;
	private UriMatcher matcher;
	
	@Override
	public boolean onCreate() {
		helper = DBOpenHelper.getInstance(getContext());
		buildUriMatcher();
		return true;
	}
	
	/**
	 * 建立UriMatcher
	 */
	private void buildUriMatcher() {
		matcher = new UriMatcher(UriMatcher.NO_MATCH);
		matcher.addURI(AUTHORITY, TBNAME, MACADDR);
		matcher.addURI(AUTHORITY, TBNAME+"/#", MACADDR_ID);
		matcher.addURI(AUTHORITY, TBNAME+"/#/name", MACADDR_NAME);
		matcher.addURI(AUTHORITY, TBNAME+"/#/mac", MACADDR_MAC);
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Cursor cursor = null;
		SQLiteDatabase db = helper.getReadableDatabase();
		if(db.isOpen()){
			switch(matcher.match(uri)){
			case MACADDR:
				System.out.println("MACADDR");
				//cursor = db.query(TBNAME, projection, selection, selectionArgs, null, null, null, null);
				break;
			case MACADDR_ID:
				System.out.println("MACADDR_ID");
				break;
			case MACADDR_NAME:
				System.out.println("MACADDR_NAME");
				break;
			case MACADDR_MAC:
				System.out.println("MACADDR_MAC");
				break;
			default:
				break;
			}
		}
		return cursor;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		return null;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		return 0;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		return 0;
	}

}
